using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using RSSRadio.Model;

namespace RSSRadio
{
	class PodcastListView : CoolListView, IPodcastUpdateListener
	{
		public PodcastListView()
			: base()
		{
			LoadPodcasts();

			if (Items.Count == 0)
				MessageBox.Show("No podcasts available. You can transfer them from the desktop application, or add an rss feed from the menu.");

			this.KeyDown += new KeyEventHandler(PodcastListView_KeyDown);
			this.MouseDown += new MouseEventHandler(PodcastListView_MouseDown);

			// set up the tap and hold menu
			try
			{
				ContextMenu tapAndHoldMenu = new ContextMenu();
				tapAndHoldMenu.Popup += new EventHandler(tapAndHoldMenu_Popup);
				MenuItem mnuDetails = new MenuItem();
				mnuDetails.Text = "Details";
				mnuDetails.Click += new EventHandler(mnuDetails_Click);
				MenuItem mnuDelete = new MenuItem();
				mnuDelete.Text = "Delete";
				mnuDelete.Click += new EventHandler(mnuDelete_Click);
				tapAndHoldMenu.MenuItems.Add(mnuDetails);
				tapAndHoldMenu.MenuItems.Add(mnuDelete);
				this.ContextMenu = tapAndHoldMenu;  // throws exception on smartphone
			}
			catch
			{}


            Globals.DownloadedPodcasts.addListener(this);
		}

		void mnuDelete_Click(object sender, EventArgs e)
		{
			DeleteSelectedPodcast();
		}

		public void DeleteSelectedPodcast()
		{
			if (Items.Count == 0)
				return;

			if (MessageBox.Show("Delete Podcast?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
			{
                Globals.DownloadedPodcasts.RemoveItem((PodcastInfo)Globals.DownloadedPodcasts.PodcastInfo[this.SelectedItemIndex]);
			}
		}

		void mnuDetails_Click(object sender, EventArgs e)
		{
			ShowSelectedPodcastDetails();
		}

		public void ShowSelectedPodcastDetails()
		{
			if (Items.Count == 0)
				return;

			EpisodeDetailsForm details = new EpisodeDetailsForm(Globals.DownloadedPodcasts[SelectedItemIndex]);
			details.Closing += new CancelEventHandler(ReloadItemsAndRepaint);
			details.ShowDialog();
		}


		void PodcastListView_MouseDown(object sender, MouseEventArgs e)
		{
			int itemIndex = (e.Y / ItemHeightPixels) + ScrollBar.Value;
			int maxItemsInView = (Items.Count < MaxDisplayableItems) ? Items.Count : MaxDisplayableItems;
			bool clickedValidItem = (itemIndex < ScrollBar.Value + maxItemsInView);

			if (clickedValidItem)
				SelectedItemIndex = itemIndex;

			this.Refresh();

			// don't play when called from tap and hold context menu
			if (sender != null && clickedValidItem)
				PodcastActions.Play(((PodcastInfo)Globals.DownloadedPodcasts.PodcastInfo[this.SelectedItemIndex]).FilePath);
		}
		void PodcastListView_KeyDown(object sender, KeyEventArgs e)
		{
            ProxyKeyDown(e);
		}

        public override void ProxyKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                PodcastActions.Play(((PodcastInfo)Globals.DownloadedPodcasts.PodcastInfo[this.SelectedItemIndex]).FilePath);
            else
            {
                base.ProxyKeyDown(e);
            }
        }

		void tapAndHoldMenu_Popup(object sender, EventArgs e)
		{
			PodcastListView_MouseDown(null, new MouseEventArgs(MouseButtons.None, 0, 0, this.Parent.PointToClient(MousePosition).Y, 0));
		}

		public void ReloadItemsAndRepaint(object sender, CancelEventArgs e)
		{
			this.ReloadItemsAndRepaint();
		}

        private delegate void refreshDelegate();

		public void ReloadItemsAndRepaint()
		{
			// reload items
			Items.Clear();
			LoadPodcasts();

            Refresh();
		}
        public void LoadPodcasts()
        {
            foreach (PodcastInfo info in Globals.DownloadedPodcasts.PodcastInfo)
            {
                string date;
                DateTime dt = info.PubDateToDateTime();
                if (dt != DateTime.MinValue)
                {
                    date = dt.Month.ToString() + "/" + dt.Day.ToString();
                }
                else
                {
                    date = "";
                }

                CoolItem item = new CoolItem();
                if (MediaFileUtil.IsVideo(MediaFileUtil.GetFileType(info.FilePath)))
                    item.setIconResourceStrings("RSSRadio.graphics.film.bmp", 
                        "RSSRadio.graphics.film32.bmp");
                else
                    item.setIconResourceStrings("RSSRadio.graphics.speaker.bmp", 
                        "RSSRadio.graphics.speaker32.bmp");
                item.Title = date + " " + info.FeedName;
                item.Description = info.Details.Title;
                this.Items.Add(item);
            }
        }


        #region IPodcastUpdateListener Members

        public void PodcastAdded(PodcastInfo info)
        {
            this.Invoke(new refreshDelegate(ReloadItemsAndRepaint));
        }

        public void PodcastRemoved(PodcastInfo info)
        {
            this.Invoke(new refreshDelegate(ReloadItemsAndRepaint));
        }

        #endregion

    }
}
